home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy-1.1 (sources only) / mindy-1.1 / comp / src.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-19  |  41.2 KB  |  1,936 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: src.c,v 1.22 94/08/18 21:35:51 wlott Exp $
  27. *
  28. * This file implements the various nodes in the parse tree.
  29. *
  30. \**********************************************************************/
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. #include "mindycomp.h"
  37. #include "sym.h"
  38. #include "lexer.h"
  39. #include "literal.h"
  40. #include "src.h"
  41. #include "info.h"
  42. #include "lose.h"
  43.  
  44. struct local_methods {
  45.     struct method *head;
  46.     struct method **tail;
  47. };
  48.  
  49. struct binop_series {
  50.     struct binop *head;
  51.     struct binop **tail;
  52. };
  53.  
  54. struct arglist {
  55.     struct argument *head;
  56.     struct argument **tail;
  57. };
  58.  
  59. struct block_epilog {
  60.     struct exception_clause *inner;
  61.     struct body *cleanup;
  62.     struct exception_clause *outer;
  63. };
  64.  
  65. struct incomplete_condition_body {
  66.     struct constituent *constituents;
  67.     struct condition_body *rest;
  68. };
  69.  
  70. struct exception_clauses {
  71.     struct exception_clause *head;
  72.     struct exception_clause **tail;
  73. };
  74.  
  75. struct superclass_list {
  76.     struct superclass *head;
  77.     struct superclass **tail;
  78. };
  79.  
  80. struct for_header {
  81.     struct for_clause *clauses;
  82.     struct expr *until;
  83. };
  84.  
  85. struct gf_suffix {
  86.     struct return_type_list *rettypes;
  87.     struct plist *plist;
  88. };
  89.  
  90. struct to_part {
  91.     enum to_kind kind;
  92.     struct expr *expr;
  93. };
  94.  
  95. struct class_guts {
  96.     struct slot_spec *slots;
  97.     struct slot_spec **slots_tail;
  98.     struct initarg_spec *initargs;
  99.     struct initarg_spec **initargs_tail;
  100.     struct inherited_spec *inheriteds;
  101.     struct inherited_spec **inheriteds_tail;
  102. };
  103.  
  104. struct else_part {
  105.     int else_line;
  106.     struct body *alternate;
  107. };
  108.  
  109. struct body *make_body(void)
  110. {
  111.     struct body *res = malloc(sizeof(struct body));
  112.  
  113.     res->head = NULL;
  114.     res->tail = &res->head;
  115.  
  116.     return res;
  117. }
  118.  
  119. struct body
  120.     *add_constituent(struct body *body, struct constituent *constituent)
  121. {
  122.     if (constituent->kind == constituent_EXPR) {
  123.     struct expr *expr = ((struct expr_constituent *)constituent)->expr;
  124.     if (expr->kind == expr_BODY) {
  125.         struct body *insides = ((struct body_expr *)expr)->body;
  126.         struct constituent *c, **prev;
  127.  
  128.         *body->tail = insides->head;
  129.         /* Note: we can't use insides->tail because that will point */
  130.         /* inside the bindings established inside this block */
  131.         for (prev = body->tail; (c = *prev) != NULL; prev = &c->next)
  132.         ;
  133.         body->tail = prev;
  134.  
  135.         free(insides);
  136.         free(expr);
  137.         free(constituent);
  138.  
  139.         return body;
  140.     }
  141.     }
  142.         
  143.     *body->tail = constituent;
  144.  
  145.     switch (constituent->kind) {
  146.       case constituent_LET:
  147.       case constituent_LOCAL:
  148.       case constituent_HANDLER:
  149.     body->tail = ((struct binding_constituent *)constituent)->body->tail;
  150.     ((struct binding_constituent *)constituent)->body->tail = NULL;
  151.     break;
  152.       default:
  153.     body->tail = &constituent->next;
  154.     break;
  155.     }
  156.  
  157.     return body;
  158. }
  159.  
  160. struct body *make_expr_body(struct expr *expr)
  161. {
  162.     return add_constituent(make_body(), make_expr_constituent(expr));
  163. }
  164.  
  165. struct constituent *make_define_constant(int line, struct bindings *bindings)
  166. {
  167.     struct defconst_constituent *res
  168.     = malloc(sizeof(struct defconst_constituent));
  169.  
  170.     res->kind = constituent_DEFCONST;
  171.     res->next = NULL;
  172.     res->line = line;
  173.     res->bindings = bindings;
  174.     res->tlf = NULL;
  175.  
  176.     return (struct constituent *)res;
  177. }
  178.  
  179. struct constituent *make_define_method(flags_t flags, struct method *method)
  180. {
  181.     struct defmethod_constituent *res
  182.     = malloc(sizeof(struct defmethod_constituent));
  183.  
  184.     res->kind = constituent_DEFMETHOD;
  185.     res->next = NULL;
  186.     res->flags = flags;
  187.     res->method = method;
  188.     res->tlf = NULL;
  189.  
  190.     return (struct constituent *)res;
  191. }
  192.  
  193. struct constituent *make_define_variable(int line, struct bindings *bindings)
  194. {
  195.     struct defvar_constituent *res = malloc(sizeof(struct defvar_constituent));
  196.  
  197.     res->kind = constituent_DEFVAR;
  198.     res->next = NULL;
  199.     res->line = line;
  200.     res->bindings = bindings;
  201.     res->tlf = NULL;
  202.  
  203.     return (struct constituent *)res;
  204. }
  205.  
  206. struct constituent *make_expr_constituent(struct expr *expr)
  207. {
  208.     struct expr_constituent *res = malloc(sizeof(struct expr_constituent));
  209.  
  210.     res->kind = constituent_EXPR;
  211.     res->next = NULL;
  212.     res->expr = expr;
  213.  
  214.     return (struct constituent *)res;
  215. }
  216.  
  217. struct constituent *make_let(struct bindings *bindings)
  218. {
  219.     struct let_constituent *res    = malloc(sizeof(struct let_constituent));
  220.  
  221.     res->kind = constituent_LET;
  222.     res->next = NULL;
  223.     res->body = make_body();
  224.     res->offset = 0;
  225.     res->bindings = bindings;
  226.     res->required = 0;
  227.     res->lexenv = NULL;
  228.     res->inside = NULL;
  229.  
  230.     return (struct constituent *)res;
  231. }
  232.  
  233. struct constituent
  234.     *make_handler(struct expr *type, struct expr *func, struct plist *plist)
  235. {
  236.     struct handler_constituent *res
  237.     = malloc(sizeof(struct handler_constituent));
  238.  
  239.     res->kind = constituent_HANDLER;
  240.     res->next = NULL;
  241.     res->body = make_body();
  242.     res->type = type;
  243.     res->func = func;
  244.     res->plist = plist;
  245.  
  246.     return (struct constituent *)res;
  247. }
  248.  
  249. struct constituent *make_local_constituent(struct local_methods *methods)
  250. {
  251.     struct local_constituent *res
  252.     = malloc(sizeof(struct local_constituent));
  253.  
  254.     res->kind = constituent_LOCAL;
  255.     res->next = NULL;
  256.     res->body = make_body();
  257.     res->offset = 0;
  258.     res->methods = methods->head;
  259.     res->lexenv = NULL;
  260.  
  261.     free(methods);
  262.  
  263.     return (struct constituent *)res;
  264. }
  265.  
  266. struct constituent
  267.     *make_top_level_form(char *debug_name, struct constituent *c)
  268. {
  269.     struct tlf_constituent *res = malloc(sizeof(struct tlf_constituent));
  270.  
  271.     c->next = NULL;
  272.  
  273.     res->kind = constituent_TOPLEVELFORM;
  274.     res->next = NULL;
  275.     res->form
  276.     = make_top_level_method(debug_name, add_constituent(make_body(), c));
  277.  
  278.     return (struct constituent *)res;
  279. }
  280.  
  281. struct expr *make_varref(struct id *var)
  282. {
  283.     struct varref_expr *res = malloc(sizeof(struct varref_expr));
  284.  
  285.     res->kind = expr_VARREF;
  286.     res->analized = FALSE;
  287.     res->var = var;
  288.     res->home = NULL;
  289.     res->binding = NULL;
  290.     res->over = NULL;
  291.  
  292.     return (struct expr *)res;
  293. }
  294.  
  295. struct expr *make_varset(struct id *var, struct expr *expr)
  296. {
  297.     struct varset_expr *res = malloc(sizeof(struct varset_expr));
  298.  
  299.     res->kind = expr_VARSET;
  300.     res->analized = FALSE;
  301.     res->var = var;
  302.     res->home = NULL;
  303.     res->binding = NULL;
  304.     res->over = NULL;
  305.     res->value = expr;
  306.     res->type = NULL;
  307.  
  308.     return (struct expr *)res;
  309. }
  310.  
  311. struct id *id(struct symbol *symbol)
  312. {
  313.     struct id *res = malloc(sizeof(struct id));
  314.  
  315.     res->symbol = symbol;
  316.     res->internal = TRUE;
  317.     res->line = 0;
  318.  
  319.     return res;
  320. }
  321.  
  322. struct id *dup_id(struct id *id)
  323. {
  324.     struct id *res = malloc(sizeof(*res));
  325.  
  326.     memcpy(res, id, sizeof(*res));
  327.     res->line = 0;
  328.  
  329.     return res;
  330. }
  331.  
  332. struct id *make_id(struct token *token)
  333. {
  334.     char *ptr = token->chars;
  335.     struct id *res;
  336.  
  337.     if (*ptr == '\\')
  338.     ptr++;
  339.  
  340.     res = id(symbol(ptr));
  341.     res->internal = FALSE;
  342.     res->line = token->line;
  343.  
  344.     free(token);
  345.  
  346.     return res;
  347. }
  348.  
  349. struct bindings *make_bindings(struct param_list *params, struct expr *expr)
  350. {
  351.     struct bindings *res = malloc(sizeof(struct bindings));
  352.  
  353.     res->params = params;
  354.     res->expr = expr;
  355.  
  356.     return res;
  357. }
  358.  
  359. struct param_list *make_param_list(void)
  360. {
  361.     struct param_list *res = malloc(sizeof(struct param_list));
  362.  
  363.     res->required_params = NULL;
  364.     res->next_param = NULL;
  365.     res->rest_param = NULL;
  366.     res->allow_keys = FALSE;
  367.     res->all_keys = FALSE;
  368.     res->keyword_params = NULL;
  369.  
  370.     return res;
  371. }
  372.  
  373. struct param_list *push_param(struct param *param, struct param_list *list)
  374. {
  375.     param->next = list->required_params;
  376.     list->required_params = param;
  377.  
  378.     return list;
  379. }
  380.  
  381. struct param_list *set_next_param(struct param_list *list, struct id *var)
  382. {
  383.     list->next_param = var;
  384.  
  385.     return list;
  386. }
  387.  
  388. struct param_list *set_rest_param(struct param_list *list, struct id *var)
  389. {
  390.     list->rest_param = var;
  391.  
  392.     return list;
  393. }
  394.  
  395. struct param *make_param(struct id *id, struct expr *type)
  396. {
  397.     struct param *res = malloc(sizeof(struct param));
  398.  
  399.     res->id = id;
  400.     res->type = type;
  401.     res->type_temp = NULL;
  402.     res->next = NULL;
  403.  
  404.     return res;
  405. }
  406.  
  407. struct param_list
  408.     *push_keyword_param(struct keyword_param *param, struct param_list *list)
  409. {
  410.     param->next = list->keyword_params;
  411.     list->keyword_params = param;
  412.  
  413.     return list;
  414. }
  415.  
  416. struct param_list *allow_keywords(struct param_list *param_list)
  417. {
  418.     param_list->allow_keys = TRUE;
  419.  
  420.     return param_list;
  421. }
  422.  
  423. struct param_list *allow_all_keywords(struct param_list *param_list)
  424. {
  425.     param_list->allow_keys = TRUE;
  426.     param_list->all_keys = TRUE;
  427.  
  428.     return param_list;
  429. }
  430.  
  431. struct keyword_param
  432.     *make_keyword_param(struct token *key, struct id *sym, struct expr *type,
  433.             struct expr *def)
  434. {
  435.     struct keyword_param *res = malloc(sizeof(struct keyword_param));
  436.  
  437.     if (key) {
  438.     /* The keyword token has a trailing : */
  439.     key->chars[key->length-1] = '\0';
  440.     res->keyword = symbol(key->chars);
  441.     free(key);
  442.     }
  443.     else
  444.     res->keyword = sym->symbol;
  445.  
  446.     res->id = sym;
  447.     res->type = type;
  448.     res->type_temp = NULL;
  449.     res->def = def;
  450.     res->next = NULL;
  451.  
  452.     return res;
  453. }
  454.  
  455. struct local_methods
  456.     *add_local_method(struct local_methods *methods, struct method *method)
  457. {
  458.     *methods->tail = method;
  459.     methods->tail = &method->next_local;
  460.  
  461.     return methods;
  462. }
  463.  
  464. struct local_methods *make_local_methods(void)
  465. {
  466.     struct local_methods *res = malloc(sizeof(struct local_methods));
  467.  
  468.     res->head = NULL;
  469.     res->tail = &res->head;
  470.  
  471.     return res;
  472. }
  473.  
  474. struct expr *make_literal_ref(struct literal *lit)
  475. {
  476.     struct literal_expr *res = malloc(sizeof(struct literal_expr));
  477.  
  478.     res->kind = expr_LITERAL;
  479.     res->analized = FALSE;
  480.     res->lit = lit;
  481.  
  482.     return (struct expr *)res;
  483. }
  484.  
  485. struct expr *make_binop_series_expr(struct expr *operand,
  486.                     struct binop_series *series)
  487. {
  488.     if (series->head) {
  489.     struct binop_series_expr *res
  490.         = malloc(sizeof(struct binop_series_expr));
  491.  
  492.     res->kind = expr_BINOP_SERIES;
  493.     res->analized = FALSE;
  494.     res->first_operand = operand;
  495.     res->first_binop = series->head;
  496.  
  497.     free(series);
  498.  
  499.     return (struct expr *)res;
  500.     }
  501.     else {
  502.     free(series);
  503.     return operand;
  504.     }
  505. }
  506.  
  507. struct binop_series *make_binop_series(void)
  508. {
  509.     struct binop_series *res = malloc(sizeof(struct binop_series));
  510.  
  511.     res->head = NULL;
  512.     res->tail = &res->head;
  513.  
  514.     return res;
  515. }
  516.  
  517. struct binop_series
  518.     *add_binop(struct binop_series *series, struct binop *op,
  519.            struct expr *operand)
  520. {
  521.     *series->tail = op;
  522.     series->tail = &op->next;
  523.     op->operand = operand;
  524.  
  525.     return series;
  526. }
  527.  
  528. struct binop *make_binop(struct id *id)
  529. {
  530.     struct binop *res = malloc(sizeof(struct binop));
  531.     struct binop_info *info = lookup_binop_info(id);
  532.  
  533.     res->op = id;
  534.     res->operand = NULL;
  535.     res->precedence = info->precedence;
  536.     res->left_assoc = info->left_assoc;
  537.     res->next = NULL;
  538.  
  539.     return res;
  540. }
  541.  
  542. static struct expr *make_unary_fn_call(struct expr *fn, struct expr *arg)
  543. {
  544.     struct arglist *args = make_argument_list();
  545.  
  546.     add_argument(args, make_argument(arg));
  547.  
  548.     return make_function_call(fn, args);
  549. }    
  550.  
  551. struct expr *make_negate(struct expr *expr)
  552. {
  553.     return make_unary_fn_call(make_varref(id(sym_Negative)), expr);
  554. }
  555.  
  556. static struct body *make_literal_body(struct literal *literal)
  557. {
  558.     return add_constituent(make_body(),
  559.                make_expr_constituent
  560.                    (make_literal_ref(literal)));
  561. }
  562.  
  563. struct expr *make_not(struct expr *expr)
  564. {
  565.     return make_if(expr, NULL,
  566.            make_else(0, make_literal_body(make_true_literal())));
  567. }
  568.  
  569. struct expr *make_singleton(struct expr *expr)
  570. {
  571.     return make_unary_fn_call(make_varref(id(sym_Singleton)), expr);
  572. }
  573.  
  574. struct expr *make_aref_or_element(struct expr *expr, struct arglist *args)
  575. {
  576.     struct argument *collection = make_argument(expr);
  577.  
  578.     collection->next = args->head;
  579.     args->head = collection;
  580.     /* This leaves args->tail wrong, but that doens't matter because */
  581.     /* because we just pass it directly to make_function_call */
  582.  
  583.     if (args->head->next != NULL && args->head->next->next == NULL)
  584.     return make_function_call(make_varref(id(sym_Element)), args);
  585.     else
  586.     return make_function_call(make_varref(id(sym_Aref)), args);
  587. }
  588.  
  589. struct expr *make_function_call(struct expr *expr, struct arglist *args)
  590. {
  591.     struct call_expr *res = malloc(sizeof(struct call_expr));
  592.  
  593.     res->kind = expr_CALL;
  594.     res->analized = FALSE;
  595.     res->func = expr;
  596.     if (expr->kind == expr_VARREF)
  597.     res->info = lookup_function_info(((struct varref_expr *)expr)->var,
  598.                      FALSE);
  599.     else
  600.     res->info = NULL;
  601.     res->args = args->head;
  602.  
  603.     free(args);
  604.  
  605.     return (struct expr *)res;
  606. }
  607.  
  608. struct expr *make_method_ref(struct method *method)
  609. {
  610.     struct method_expr *res = malloc(sizeof(struct method_expr));
  611.  
  612.     res->kind = expr_METHOD;
  613.     res->analized = FALSE;
  614.     res->method = method;
  615.  
  616.     return (struct expr *)res;
  617. }
  618.  
  619. struct expr *make_dot_operation(struct expr *arg, struct expr *func)
  620. {
  621.     struct dot_expr *res = malloc(sizeof(struct dot_expr));
  622.  
  623.     res->kind = expr_DOT;
  624.     res->analized = FALSE;
  625.     res->arg = arg;
  626.     res->func = func;
  627.  
  628.     return (struct expr *)res;
  629. }
  630.  
  631. struct arglist *make_argument_list(void)
  632. {
  633.     struct arglist *res = malloc(sizeof(struct arglist));
  634.  
  635.     res->head = NULL;
  636.     res->tail = &res->head;
  637.  
  638.     return res;
  639. }
  640.  
  641. struct arglist *add_argument(struct arglist *arglist, struct argument *arg)
  642. {
  643.     *arglist->tail = arg;
  644.     while (arg->next != NULL)
  645.     arg = arg->next;
  646.     arglist->tail = &arg->next;
  647.  
  648.     return arglist;
  649. }
  650.  
  651. struct argument *make_argument(struct expr *expr)
  652. {
  653.     struct argument *res = malloc(sizeof(struct argument));
  654.  
  655.     res->expr = expr;
  656.     res->next = NULL;
  657.  
  658.     return res;
  659. }
  660.  
  661. struct argument
  662.     *make_keyword_argument(struct token *keyword, struct expr *expr)
  663. {
  664.     struct argument *keyarg
  665.     = make_argument(make_literal_ref(parse_keyword_token(keyword)));
  666.  
  667.     keyarg->next = make_argument(expr);
  668.  
  669.     return keyarg;
  670. }
  671.  
  672. struct plist *make_property_list(void)
  673. {
  674.     struct plist *res = malloc(sizeof(struct plist));
  675.  
  676.     res->head = NULL;
  677.     res->tail = &res->head;
  678.  
  679.     return res;
  680. }
  681.  
  682. struct plist
  683.     *add_property(struct plist *plist, struct token *key, struct expr *expr)
  684. {
  685.     struct property *prop = malloc(sizeof(struct property));
  686.  
  687.     /* The keyword token has a trailing : */
  688.     key->chars[key->length-1] = '\0';
  689.  
  690.     prop->line = key->line;
  691.     prop->keyword = symbol(key->chars);
  692.     prop->expr = expr;
  693.     prop->next = NULL;
  694.  
  695.     *plist->tail = prop;
  696.     plist->tail = &prop->next;
  697.  
  698.     free(key);
  699.  
  700.     return plist;
  701. }
  702.  
  703. struct return_type_list *make_return_type_list(boolean restp,
  704.                            struct expr *rest)
  705. {
  706.     struct return_type_list *res = malloc(sizeof(struct return_type_list));
  707.  
  708.     res->req_types = NULL;
  709.     res->req_types_tail = &res->req_types;
  710.     res->req_types_list = NULL;
  711.     res->restp = restp;
  712.     res->rest_type = rest;
  713.     res->rest_temp = NULL;
  714.     res->rest_temp_varref = NULL;
  715.  
  716.     return res;
  717. }
  718.  
  719. struct return_type_list *add_return_type(struct return_type_list *list,
  720.                      struct expr *type)
  721. {
  722.     struct return_type *rtype = malloc(sizeof(struct return_type));
  723.  
  724.     rtype->type = type;
  725.     rtype->temp = NULL;
  726.     rtype->next = NULL;
  727.     *list->req_types_tail = rtype;
  728.     list->req_types_tail = &rtype->next;
  729.  
  730.     return list;
  731. }
  732.  
  733. struct return_type_list
  734.     *set_return_type_rest_type(struct return_type_list *list,
  735.                    struct expr *type)
  736. {
  737.     list->restp = TRUE;
  738.     list->rest_type = type;
  739.     return list;
  740. }
  741.  
  742. struct literal *parse_true_token(struct token *token)
  743. {
  744.     struct literal *res = make_true_literal();
  745.     res->line = token->line;
  746.     free(token);
  747.     return res;
  748. }
  749.  
  750. struct literal *parse_false_token(struct token *token)
  751. {
  752.     struct literal *res = make_false_literal();
  753.     res->line = token->line;
  754.     free(token);
  755.     return res;
  756. }
  757.  
  758. struct literal *parse_unbound_token(struct token *token)
  759. {
  760.     struct literal *res = make_unbound_literal();
  761.     res->line = token->line;
  762.     free(token);
  763.     return res;
  764. }
  765.  
  766. static int escape_char(int c)
  767. {
  768.     switch (c) {
  769.       case 'a': return '\007';
  770.       case 'b': return '\b';
  771.       case 'e': return '\033';
  772.       case 'f': return '\f';
  773.       case 'n': return '\n';
  774.       case 'r': return '\r';
  775.       case 't': return '\t';
  776.       case '0': return '\0';
  777.       default: return c;
  778.     }
  779. }
  780.  
  781. struct literal *parse_string_token(struct token *token)
  782. {
  783.     struct string_literal *res;
  784.     int length = token->length - 2;
  785.     int i;
  786.     char *src, *dst;
  787.  
  788.     src = token->chars + 1;
  789.     for (i = length; i > 0; i--) {
  790.     if (*src++ == '\\') {
  791.         length--;
  792.         i--;
  793.         src++;
  794.     }
  795.     }
  796.  
  797.     res = malloc(sizeof(struct string_literal) + length + 1);
  798.  
  799.     res->kind = literal_STRING;
  800.     res->next = NULL;
  801.     res->line = token->line;
  802.     res->length = length;
  803.  
  804.     src = token->chars + 1;
  805.     dst = res->chars;
  806.     for (i = length; i > 0; i--) {
  807.     int c = *src++;
  808.     if (c == '\\')
  809.         *dst++ = escape_char(*src++);
  810.     else
  811.         *dst++ = c;
  812.     }
  813.     *dst++ = '\0';
  814.  
  815.     free(token);
  816.  
  817.     return (struct literal *)res;
  818. }
  819.  
  820. struct literal
  821.     *concat_string_token(struct literal *old_literal, struct token *token)
  822. {
  823.     struct string_literal *old = (struct string_literal *)old_literal;
  824.     int old_length = old->length;
  825.     char *old_string = old->chars;
  826.     struct string_literal *res;
  827.     int length = token->length - 2;
  828.     int i;
  829.     char *src, *dst;
  830.  
  831.     res = malloc(sizeof(struct string_literal) + old_length + length + 1);
  832.  
  833.     res->kind = literal_STRING;
  834.     res->next = NULL;
  835.     res->line = old_literal->line;
  836.  
  837.     strncpy(res->chars, old_string, old_length);
  838.     src = token->chars + 1;
  839.     dst = res->chars + old_length;
  840.     for (i = 0; i < length; i++) {
  841.     int c = *src++;
  842.     if (c == '\\') {
  843.         *dst++ = escape_char(*src++);
  844.         length--;
  845.     } else
  846.         *dst++ = c;
  847.     }
  848.     *dst++ = '\0';
  849.  
  850.     res->length = length + old_length;
  851.     free(token);
  852.  
  853.     return (struct literal *)res;
  854. }
  855.  
  856. struct literal *parse_character_token(struct token *token)
  857. {
  858.     int c = token->chars[1];
  859.     struct literal *res;
  860.  
  861.     if (c == '\\')
  862.     c = escape_char(token->chars[2]);
  863.  
  864.     res = make_character_literal(c);
  865.     res->line = token->line;
  866.  
  867.     free(token);
  868.  
  869.     return res;
  870. }
  871.  
  872. struct literal *parse_integer_token(struct token *token)
  873. {
  874.     long value;
  875.     int count, radix = 0;
  876.     boolean negative;
  877.     char *ptr;
  878.     struct literal *res;
  879.  
  880.     value = 0;
  881.     count = token->length;
  882.     ptr = token->chars;
  883.     if (*ptr == '#') {
  884.     switch (ptr[1]) {
  885.       case 'x': radix = 16; break;
  886.       case 'o': radix = 8; break;
  887.       case 'b': radix = 2; break;
  888.     }
  889.     ptr += 2;
  890.     count -= 2;
  891.     negative = FALSE;
  892.     }
  893.     else {
  894.     radix = 10;
  895.     if (*ptr == '-') {
  896.         negative = TRUE;
  897.         count--;
  898.         ptr++;
  899.     }
  900.     else {
  901.         negative = FALSE;
  902.         if (*ptr == '+') {
  903.         count--;
  904.         ptr++;
  905.         }
  906.     }
  907.     }
  908.     if (radix == 0)
  909.     lose("No radix in integer literal?");
  910.  
  911.     while (count-- > 0) {
  912.     int digit = *ptr++;
  913.     if (digit >= 'a')
  914.         digit = digit - 'a' + 10;
  915.     else if (digit >= 'A')
  916.         digit = digit - 'A' + 10;
  917.     else
  918.         digit = digit - '0';
  919.     if (negative)
  920.         value = value * radix - digit;
  921.     else
  922.         value = value * radix + digit;
  923.     }
  924.  
  925.     res = make_integer_literal(value);
  926.     res->line = token->line;
  927.  
  928.     free(token);
  929.  
  930.     return res;
  931. }
  932.  
  933. struct literal *parse_float_token(struct token *token)
  934. {
  935.     unsigned char c, *ptr;
  936.     enum literal_kind kind = literal_SINGLE_FLOAT;
  937.     struct literal *res = NULL;
  938.  
  939.     for (ptr = token->chars; (c = *ptr) != '\0'; ptr++) {
  940.     if (c == 'e' || c == 'E')
  941.         break;
  942.     if (c == 's' || c == 'S') {
  943.         *ptr = 'e';
  944.         break;
  945.     }
  946.     if (c == 'd' || c == 'D') {
  947.         *ptr = 'e';
  948.         kind = literal_DOUBLE_FLOAT;
  949.         break;
  950.     }
  951.     if (c == 'x' || c == 'X') {
  952.         *ptr = 'e';
  953.         kind = literal_EXTENDED_FLOAT;
  954.         break;
  955.     }
  956.     }
  957.  
  958.     switch (kind) {
  959.       case literal_SINGLE_FLOAT:
  960.     {
  961.         struct single_float_literal *r = malloc(sizeof(*r));
  962.         res = (struct literal *)r;
  963.         r->value = atof(token->chars);
  964.         break;
  965.     }
  966.       case literal_DOUBLE_FLOAT:
  967.     {
  968.         struct double_float_literal *r = malloc(sizeof(*r));
  969.         res = (struct literal *)r;
  970.         r->value = atof(token->chars);
  971.         break;
  972.     }
  973.       case literal_EXTENDED_FLOAT:
  974.     {
  975.         struct extended_float_literal *r = malloc(sizeof(*r));
  976.         res = (struct literal *)r;
  977.         r->value = atof(token->chars);
  978.         break;
  979.     }
  980.       default:
  981.     lose("Strange float literal kind.\n");
  982.     break;
  983.     }
  984.  
  985.     res->kind = kind;
  986.     res->next = NULL;
  987.     res->line = token->line;
  988.  
  989.     free(token);
  990.  
  991.     return res;
  992. }
  993.  
  994. struct literal *parse_symbol_token(struct token *token)
  995. {
  996.     char *ptr = token->chars;
  997.     struct literal *res;
  998.  
  999.     /* We modify the token here, but we don't care 'cause we will be */
  1000.     /* freeing it shortly. */
  1001.  
  1002.     if (*ptr == '\\')
  1003.     /* They used the \op quoting convention. */
  1004.     ptr++;
  1005.  
  1006.     res = make_symbol_literal(symbol(ptr));
  1007.     res->line = token->line;
  1008.  
  1009.     free(token);
  1010.  
  1011.     return res;
  1012. }
  1013.  
  1014. struct literal *parse_keyword_token(struct token *token)
  1015. {
  1016.     char *ptr = token->chars;
  1017.     struct literal *res;
  1018.  
  1019.     /* We modify the token here, but we don't care 'cause we will be */
  1020.     /* freeing it shortly. */
  1021.  
  1022.     /* keyword tokens have a trailing : or " */
  1023.     ptr[token->length-1] = '\0';
  1024.  
  1025.     /* Sometimes they also have a leading #" */
  1026.     if (*ptr == '#')
  1027.     ptr += 2;
  1028.  
  1029.     res = make_symbol_literal(symbol(ptr));
  1030.     res->line = token->line;
  1031.  
  1032.     free(token);
  1033.  
  1034.     return res;
  1035. }
  1036.  
  1037. struct expr *make_body_expr(struct body *body)
  1038. {
  1039.     if (body->head && body->head->kind == constituent_EXPR
  1040.       && body->head->next == NULL) {
  1041.     struct expr *res = ((struct expr_constituent *)body->head)->expr;
  1042.     free(body->head);
  1043.     free(body);
  1044.     return res;
  1045.     }
  1046.     else {
  1047.     struct body_expr *res = malloc(sizeof(struct body_expr));
  1048.  
  1049.     res->kind = expr_BODY;
  1050.     res->analized = FALSE;
  1051.     res->body = body;
  1052.  
  1053.     return (struct expr *)res;
  1054.     }
  1055. }
  1056.  
  1057. struct expr *make_block(int line, struct id *exit, struct body *body,
  1058.             struct block_epilog *epilog)
  1059. {
  1060.     struct block_expr *res = malloc(sizeof(struct block_expr));
  1061.  
  1062.     res->kind = expr_BLOCK;
  1063.     res->analized = FALSE;
  1064.     res->line = line;
  1065.     res->exit_fun = exit;
  1066.     res->body = body;
  1067.     if (epilog) {
  1068.     res->inner = epilog->inner;
  1069.     res->cleanup = epilog->cleanup;
  1070.     res->outer = epilog->outer;
  1071.     free(epilog);
  1072.     }
  1073.     else {
  1074.     res->inner = NULL;
  1075.     res->cleanup = NULL;
  1076.     res->outer = NULL;
  1077.     }
  1078.  
  1079.     return (struct expr *)res;
  1080. }
  1081.  
  1082. struct expr *make_case(struct condition_body *body)
  1083. {
  1084.     struct case_expr *res = malloc(sizeof(struct case_expr));
  1085.  
  1086.     res->kind = expr_CASE;
  1087.     res->analized = FALSE;
  1088.     res->body = body;
  1089.  
  1090.     return (struct expr *)res;
  1091. }
  1092.  
  1093. struct expr *make_if(struct expr *cond, struct body *consequent,
  1094.              struct else_part *else_part)
  1095. {
  1096.     struct if_expr *res = malloc(sizeof(struct if_expr));
  1097.  
  1098.     res->kind = expr_IF;
  1099.     res->analized = FALSE;
  1100.     res->cond = cond;
  1101.     if (consequent)
  1102.     res->consequent = consequent;
  1103.     else
  1104.     res->consequent = make_literal_body(make_false_literal());
  1105.     if (else_part) {
  1106.     res->else_line = else_part->else_line;
  1107.     res->alternate = else_part->alternate;
  1108.     free(else_part);
  1109.     }
  1110.     else {
  1111.     res->else_line = 0;
  1112.     res->alternate = make_literal_body(make_false_literal());
  1113.     }
  1114.  
  1115.     return (struct expr *)res;
  1116. }
  1117.  
  1118. struct else_part *make_else(int else_line, struct body *alternate)
  1119. {
  1120.     struct else_part *res = malloc(sizeof(*res));
  1121.  
  1122.     res->else_line = else_line;
  1123.     res->alternate = alternate;
  1124.  
  1125.     return res;
  1126. }
  1127.  
  1128. struct expr *make_for(struct for_header *header, struct body *body,
  1129.                  struct body *finally)
  1130. {
  1131.     struct for_expr *res = malloc(sizeof(struct for_expr));
  1132.  
  1133.     res->kind = expr_FOR;
  1134.     res->analized = FALSE;
  1135.     res->clauses = header->clauses;
  1136.     res->until = header->until;
  1137.     res->body = body;
  1138.     res->finally = finally;
  1139.  
  1140.     free(header);
  1141.  
  1142.     return (struct expr *)res;
  1143. }
  1144.  
  1145. struct expr *make_select(struct expr *expr, struct expr *by,
  1146.                 struct condition_body *body)
  1147. {
  1148.     struct select_expr *res = malloc(sizeof(struct select_expr));
  1149.  
  1150.     res->kind = expr_SELECT;
  1151.     res->analized = FALSE;
  1152.     res->expr = expr;
  1153.     res->by = by;
  1154.     res->body = body;
  1155.  
  1156.     return (struct expr *)res;
  1157. }
  1158.  
  1159. struct expr *make_loop(struct body *body)
  1160. {
  1161.     struct loop_expr *res = malloc(sizeof(struct loop_expr));
  1162.  
  1163.     res->kind = expr_LOOP;
  1164.     res->analized = FALSE;
  1165.     res->body = body;
  1166.     res->position = 0;
  1167.  
  1168.     return (struct expr *)res;
  1169. }
  1170.  
  1171. struct expr *make_repeat(void)
  1172. {
  1173.     struct repeat_expr *res = malloc(sizeof(struct repeat_expr));
  1174.  
  1175.     res->kind = expr_REPEAT;
  1176.     res->analized = FALSE;
  1177.     res->loop = NULL;
  1178.  
  1179.     return (struct expr *)res;
  1180. }
  1181.  
  1182. struct block_epilog *make_block_epilog(struct exception_clauses *inner,
  1183.                        struct body *cleanup,
  1184.                        struct exception_clauses *outer)
  1185. {
  1186.     struct block_epilog *res = malloc(sizeof(struct block_epilog));
  1187.  
  1188.     if (inner) {
  1189.     res->inner = inner->head;
  1190.     free(inner);
  1191.     }
  1192.     else
  1193.     res->inner = NULL;
  1194.     res->cleanup = cleanup;
  1195.     if (outer) {
  1196.     res->outer = outer->head;
  1197.     free(outer);
  1198.     }
  1199.     else
  1200.     res->outer = NULL;
  1201.  
  1202.     return res;
  1203. }
  1204.  
  1205. struct for_header *make_for_header(struct expr *until)
  1206. {
  1207.     struct for_header *res = malloc(sizeof(struct for_header));
  1208.  
  1209.     res->clauses = NULL;
  1210.     res->until = until;
  1211.  
  1212.     return res;
  1213. }
  1214.  
  1215. struct for_header *push_for_clause(struct for_clause *clause,
  1216.                    struct for_header *header)
  1217. {
  1218.     clause->next = header->clauses;
  1219.     header->clauses = clause;
  1220.  
  1221.     return header;
  1222. }
  1223.  
  1224. struct exception_clauses *make_exception_clauses(void)
  1225. {
  1226.     struct exception_clauses *res = malloc(sizeof(struct exception_clauses));
  1227.  
  1228.     res->head = NULL;
  1229.     res->tail = &res->head;
  1230.  
  1231.     return res;
  1232. }
  1233.  
  1234. struct exception_clauses
  1235.     *add_exception_clause(struct exception_clauses *clauses,
  1236.               struct exception_clause *clause)
  1237. {
  1238.     *clauses->tail = clause;
  1239.     clauses->tail = &clause->next;
  1240.  
  1241.     return clauses;
  1242. }
  1243.  
  1244. struct exception_clause
  1245.     *make_exception_clause(struct expr *type, struct id *condition,
  1246.                struct plist *plist, struct body *body)
  1247. {
  1248.     struct exception_clause *res = malloc(sizeof(struct exception_clause));
  1249.  
  1250.     res->type = type;
  1251.     res->condition = condition;
  1252.     res->plist = plist;
  1253.     res->body = body;
  1254.     res->next = NULL;
  1255.  
  1256.     return res;
  1257. }
  1258.  
  1259. struct condition_body
  1260.     *push_condition_clause(struct condition_clause *clause,
  1261.                struct condition_body *cond_body)
  1262. {
  1263.     struct condition_body *res = malloc(sizeof(struct condition_body));
  1264.  
  1265.     res->clause = clause;
  1266.     res->next = cond_body;
  1267.  
  1268.     return res;
  1269. }
  1270.  
  1271. struct condition_clause
  1272.     *make_otherwise_condition_clause(struct body *body)
  1273. {
  1274.     struct condition_clause *res = malloc(sizeof(struct condition_clause));
  1275.  
  1276.     res->conditions = NULL;
  1277.     res->body = body;
  1278.  
  1279.     return res;
  1280. }
  1281.  
  1282. struct incomplete_condition_body
  1283.     *make_incomplete_condition_clauses(struct constituent *constituent,
  1284.                        struct condition_body *rest)
  1285. {
  1286.     struct incomplete_condition_body *res
  1287.     = malloc(sizeof(struct incomplete_condition_body));
  1288.  
  1289.     res->constituents = constituent;
  1290.     res->rest = rest;
  1291.  
  1292.     return res;
  1293. }
  1294.  
  1295. struct incomplete_condition_body
  1296.     *push_condition_constituent(struct constituent *constituent,
  1297.                 struct incomplete_condition_body *body)
  1298. {
  1299.     constituent->next = body->constituents;
  1300.     body->constituents = constituent;
  1301.  
  1302.     return body;
  1303. }
  1304.  
  1305. struct condition_body
  1306.     *complete_condition_clauses(struct condition_clause *clause,
  1307.                 struct incomplete_condition_body *body)
  1308. {
  1309.     struct constituent *constit, *next;
  1310.     struct condition_body *res;
  1311.  
  1312.     for (constit = body->constituents; constit != NULL; constit = next) {
  1313.     next = constit->next;
  1314.     constit->next = NULL;
  1315.     add_constituent(clause->body, constit);
  1316.     }
  1317.     res = push_condition_clause(clause, body->rest);
  1318.  
  1319.     free(body);
  1320.  
  1321.     return res;
  1322. }
  1323.  
  1324. struct condition_clause
  1325.     *make_condition_clause(struct constituent *constituent)
  1326. {
  1327.     struct condition_clause *res = malloc(sizeof(struct condition_clause));
  1328.  
  1329.     res->conditions = NULL;
  1330.     res->body = add_constituent(make_body(), constituent);
  1331.  
  1332.     return res;
  1333. }
  1334.  
  1335. struct condition_clause
  1336.     *push_condition(struct expr *expr, struct condition_clause *clause)
  1337. {
  1338.     struct condition *cond = malloc(sizeof(struct condition));
  1339.  
  1340.     cond->cond = expr;
  1341.     cond->next = clause->conditions;
  1342.     clause->conditions = cond;
  1343.  
  1344.     return clause;
  1345. }
  1346.  
  1347. struct for_clause
  1348.     *make_equal_then_for_clause(struct param_list *vars, struct expr *equal,
  1349.                 struct expr *then)
  1350. {
  1351.     struct equal_then_for_clause *res
  1352.     = malloc(sizeof(struct equal_then_for_clause));
  1353.  
  1354.     res->kind = for_EQUAL_THEN;
  1355.     res->next = NULL;
  1356.     res->vars = vars;
  1357.     res->equal = equal;
  1358.     res->then = then;
  1359.  
  1360.     return (struct for_clause *)res;
  1361. }
  1362.  
  1363. struct for_clause
  1364.     *make_in_for_clause(struct param *var, struct param *keyed_by,
  1365.             struct expr *collection)
  1366. {
  1367.     struct in_for_clause *res = malloc(sizeof(*res));
  1368.     struct param_list *vars = make_param_list();
  1369.  
  1370.     if (keyed_by)
  1371.     push_param(keyed_by, vars);
  1372.     push_param(var, vars);
  1373.  
  1374.     res->kind = for_IN;
  1375.     res->next = NULL;
  1376.     res->vars = vars;
  1377.     res->collection = collection;
  1378.  
  1379.     return (struct for_clause *)res;
  1380. }
  1381.  
  1382. struct for_clause
  1383.     *make_from_for_clause(struct param *var, struct expr *from,
  1384.               struct to_part *to, struct expr *by)
  1385. {
  1386.     struct from_for_clause *res
  1387.     = malloc(sizeof(struct from_for_clause));
  1388.  
  1389.     res->kind = for_FROM;
  1390.     res->next = NULL;
  1391.     res->vars = push_param(var, make_param_list());
  1392.     res->from = from;
  1393.     if (to) {
  1394.     res->to_kind = to->kind;
  1395.     res->to = to->expr;
  1396.     free(to);
  1397.     }
  1398.     else {
  1399.     res->to_kind = to_UNBOUNDED;
  1400.     res->to = NULL;
  1401.     }
  1402.     res->by = by;
  1403.  
  1404.     return (struct for_clause *)res;
  1405. }
  1406.  
  1407. struct to_part *make_to(struct expr *expr)
  1408. {
  1409.     struct to_part *res = malloc(sizeof(struct to_part));
  1410.  
  1411.     res->kind = to_TO;
  1412.     res->expr = expr;
  1413.  
  1414.     return res;
  1415. }
  1416.  
  1417. struct to_part *make_above(struct expr *expr)
  1418. {
  1419.     struct to_part *res = malloc(sizeof(struct to_part));
  1420.  
  1421.     res->kind = to_ABOVE;
  1422.     res->expr = expr;
  1423.  
  1424.     return res;
  1425. }
  1426.  
  1427. struct to_part *make_below(struct expr *expr)
  1428. {
  1429.     struct to_part *res = malloc(sizeof(struct to_part));
  1430.  
  1431.     res->kind = to_BELOW;
  1432.     res->expr = expr;
  1433.  
  1434.     return res;
  1435. }
  1436.  
  1437. struct constituent
  1438.     *make_class_definition(struct id *name, struct superclass_list *supers,
  1439.                struct class_guts *guts)
  1440. {
  1441.     struct defclass_constituent *res
  1442.     = malloc(sizeof(struct defclass_constituent));
  1443.  
  1444.     res->kind = constituent_DEFCLASS;
  1445.     res->next = NULL;
  1446.     res->flags = 0;
  1447.     res->name = name;
  1448.     res->supers = supers->head;
  1449.     free(supers);
  1450.     if (guts) {
  1451.     res->slots = guts->slots;
  1452.     res->initargs = guts->initargs;
  1453.     res->inheriteds = guts->inheriteds;
  1454.     free(guts);
  1455.     }
  1456.     else {
  1457.     res->slots = NULL;
  1458.     res->initargs = NULL;
  1459.     res->inheriteds = NULL;
  1460.     }
  1461.     res->tlf1 = NULL;
  1462.     res->tlf2 = NULL;
  1463.  
  1464.     return (struct constituent *)res;
  1465. }
  1466.  
  1467. struct constituent *set_class_flags(flags_t flags,
  1468.                     struct constituent *defclass)
  1469. {
  1470.     ((struct defclass_constituent *)defclass)->flags = flags;
  1471.     return defclass;
  1472. }
  1473.  
  1474. struct superclass_list *make_superclass_list(void)
  1475. {
  1476.     struct superclass_list *res = malloc(sizeof(struct superclass_list));
  1477.  
  1478.     res->head = NULL;
  1479.     res->tail = &res->head;
  1480.  
  1481.     return res;
  1482. }
  1483.  
  1484. struct superclass_list
  1485.     *add_superclass(struct superclass_list *list, struct expr *expr)
  1486. {
  1487.     struct superclass *sup = malloc(sizeof(struct superclass));
  1488.  
  1489.     sup->expr = expr;
  1490.     sup->next = NULL;
  1491.  
  1492.     *list->tail = sup;
  1493.     list->tail = &sup->next;
  1494.  
  1495.     return list;
  1496. }
  1497.  
  1498. struct class_guts *make_class_guts(void)
  1499. {
  1500.     struct class_guts *res = malloc(sizeof(*res));
  1501.  
  1502.     res->slots = NULL;
  1503.     res->slots_tail = &res->slots;
  1504.     res->initargs = NULL;
  1505.     res->initargs_tail = &res->initargs;
  1506.     res->inheriteds = NULL;
  1507.     res->inheriteds_tail = &res->inheriteds;
  1508.  
  1509.     return res;
  1510. }
  1511.  
  1512. struct slot_spec
  1513.     *make_slot_spec(int line, flags_t flags, enum slot_allocation alloc,
  1514.             struct id *name, struct expr *type, struct plist *plist)
  1515. {
  1516.     struct slot_spec *res = malloc(sizeof(struct slot_spec));
  1517.  
  1518.     res->line = line;
  1519.     res->flags = flags;
  1520.     res->alloc = alloc;
  1521.     res->name = name;
  1522.     res->type = type;
  1523.     res->plist = plist;
  1524.     res->getter = NULL;
  1525.     res->setter = NULL;
  1526.     res->next = NULL;
  1527.  
  1528.     return res;
  1529. }
  1530.  
  1531. struct class_guts *add_slot_spec(struct class_guts *guts,
  1532.                  struct slot_spec *spec)
  1533. {
  1534.     *guts->slots_tail = spec;
  1535.     guts->slots_tail = &spec->next;
  1536.  
  1537.     return guts;
  1538. }
  1539.  
  1540. struct initarg_spec
  1541.   *make_initarg_spec(boolean required, struct token *key, struct plist *plist)
  1542. {
  1543.     struct initarg_spec *res = malloc(sizeof(*res));
  1544.  
  1545.     /* The keyword token has a trailing : */
  1546.     key->chars[key->length-1] = '\0';
  1547.  
  1548.     res->required = required;
  1549.     res->keyword = symbol(key->chars);
  1550.     res->plist = plist;
  1551.     res->next = NULL;
  1552.  
  1553.     return res;
  1554. }
  1555.  
  1556. struct class_guts *add_initarg_spec(struct class_guts *guts,
  1557.                     struct initarg_spec *spec)
  1558. {
  1559.     *guts->initargs_tail = spec;
  1560.     guts->initargs_tail = &spec->next;
  1561.  
  1562.     return guts;
  1563. }
  1564.  
  1565. struct inherited_spec *make_inherited_spec(struct id *name,
  1566.                        struct plist *plist)
  1567. {
  1568.     struct inherited_spec *res = malloc(sizeof(*res));
  1569.  
  1570.     res->name = name;
  1571.     res->plist = plist;
  1572.     res->next = NULL;
  1573.  
  1574.     return res;
  1575. }
  1576.  
  1577. struct class_guts *add_inherited_spec(struct class_guts *guts,
  1578.                       struct inherited_spec *spec)
  1579. {
  1580.     *guts->inheriteds_tail = spec;
  1581.     guts->inheriteds_tail = &spec->next;
  1582.  
  1583.     return guts;
  1584. }
  1585.  
  1586. struct constituent
  1587.     *make_define_generic(struct id *name, struct param_list *params,
  1588.              struct gf_suffix *suffix)
  1589. {
  1590.     struct defgeneric_constituent *res
  1591.     = malloc(sizeof(struct defgeneric_constituent));
  1592.  
  1593.     res->kind = constituent_DEFGENERIC;
  1594.     res->next = NULL;
  1595.     res->flags = 0;
  1596.     res->name = name;
  1597.     res->params = params;
  1598.     res->rettypes = suffix->rettypes;
  1599.     res->plist = suffix->plist;
  1600.     res->tlf = NULL;
  1601.  
  1602.     free(suffix);
  1603.  
  1604.     return (struct constituent *)res;
  1605. }
  1606.  
  1607. struct constituent *set_generic_flags(flags_t flags,
  1608.                     struct constituent *defgeneric)
  1609. {
  1610.     ((struct defgeneric_constituent *)defgeneric)->flags = flags;
  1611.     return defgeneric;
  1612. }
  1613.  
  1614. struct gf_suffix
  1615.     *make_gf_suffix(struct return_type_list *rettypes,
  1616.             struct plist *plist)
  1617. {
  1618.     struct gf_suffix *res = malloc(sizeof(struct gf_suffix));
  1619.  
  1620.     res->rettypes = rettypes;
  1621.     res->plist = plist;
  1622.  
  1623.     return res;
  1624. }
  1625.  
  1626. struct method *set_method_source(struct token *source, struct method *method)
  1627. {
  1628.     method->line = source->line;
  1629.  
  1630.     return method;
  1631. }
  1632.  
  1633. struct method *set_method_name(struct id *name, struct method *method)
  1634. {
  1635.     method->name = name;
  1636.     method->line = name->line;
  1637.     method->debug_name = make_symbol_literal(name->symbol);
  1638.  
  1639.     return method;
  1640. }
  1641.  
  1642. struct method
  1643.     *make_method_description(struct param_list *params,
  1644.                  struct return_type_list *rettypes,
  1645.                  struct body *body)
  1646. {
  1647.     struct method *res = malloc(sizeof(struct method));
  1648.  
  1649.     res->name = NULL;
  1650.     res->line = 0;
  1651.     res->debug_name = NULL;
  1652.     res->top_level = FALSE;
  1653.     res->component = NULL;
  1654.     res->params = params;
  1655.     res->specializers = NULL;
  1656.     res->rettypes = rettypes;
  1657.     res->body = body;
  1658.     res->next_local = NULL;
  1659.     res->nargs = 0;
  1660.     res->lexenv = NULL;
  1661.     res->frame_size = 0;
  1662.     res->closes_over = NULL;
  1663.     res->lexenv_size = 0;
  1664.     res->parent = NULL;
  1665.     res->kids = NULL;
  1666.     res->next = NULL;
  1667.  
  1668.     return res;
  1669. }
  1670.  
  1671. struct method *make_top_level_method(char *debug_name, struct body *body)
  1672. {
  1673.     struct method *res = make_method_description(make_param_list(),NULL,body);
  1674.  
  1675.     res->debug_name = make_string_literal(debug_name);
  1676.     res->top_level = TRUE;
  1677.     res->specializers=make_literal_ref(make_list_literal(make_literal_list()));
  1678.  
  1679.     return res;
  1680. }
  1681.  
  1682. struct constituent *make_error_constituent(void)
  1683. {
  1684.     struct constituent *res = malloc(sizeof(struct constituent));
  1685.  
  1686.     res->kind = constituent_ERROR;
  1687.     res->next = NULL;
  1688.  
  1689.     return res;
  1690. }
  1691.  
  1692. struct expr *make_error_expression(void)
  1693. {
  1694.     struct expr *res = malloc(sizeof(struct expr));
  1695.  
  1696.     res->kind = expr_ERROR;
  1697.     res->analized = FALSE;
  1698.  
  1699.     return res;
  1700. }
  1701.  
  1702. struct defnamespace_constituent
  1703.     *make_define_namespace(enum constituent_kind kind)
  1704. {
  1705.     struct defnamespace_constituent *res = malloc(sizeof(*res));
  1706.  
  1707.     res->kind = kind;
  1708.     res->next = NULL;
  1709.     res->name = NULL;
  1710.     res->use_clauses = NULL;
  1711.     res->use_tail = &res->use_clauses;
  1712.     res->exported_variables = make_variable_names();
  1713.     res->created_variables = make_variable_names();
  1714.     res->exported_literal = NULL;
  1715.     res->created_literal = NULL;
  1716.  
  1717.     return res;
  1718. }
  1719.  
  1720. struct defnamespace_constituent *make_define_module(void)
  1721. {
  1722.     return make_define_namespace(constituent_DEFMODULE);
  1723. }
  1724.  
  1725. struct defnamespace_constituent *make_define_library(void)
  1726. {
  1727.     return make_define_namespace(constituent_DEFLIBRARY);
  1728. }
  1729.  
  1730. struct defnamespace_constituent
  1731.     *set_namespace_name(struct defnamespace_constituent *namespace,
  1732.             struct token *name)
  1733. {
  1734.     namespace->name = parse_symbol_token(name);
  1735.  
  1736.     return namespace;
  1737. }
  1738.  
  1739. struct defnamespace_constituent
  1740.     *add_use_clause(struct defnamespace_constituent *namespace,
  1741.             struct use_clause *clause)
  1742. {
  1743.     *namespace->use_tail = clause;
  1744.     namespace->use_tail = &clause->next;
  1745.  
  1746.     return namespace;
  1747. }
  1748.  
  1749. struct defnamespace_constituent
  1750.     *add_exports(struct defnamespace_constituent *namespace,
  1751.          struct variable_names *vars)
  1752. {
  1753.     *namespace->exported_variables->tail = vars->head;
  1754.     namespace->exported_variables->tail = vars->tail;
  1755.     free(vars);
  1756.  
  1757.     return namespace;
  1758. }
  1759.  
  1760. struct defnamespace_constituent
  1761.     *add_creates(struct defnamespace_constituent *namespace,
  1762.          struct variable_names *vars)
  1763. {
  1764.     *namespace->created_variables->tail = vars->head;
  1765.     namespace->created_variables->tail = vars->tail;
  1766.     free(vars);
  1767.  
  1768.     return namespace;
  1769. }
  1770.  
  1771. struct use_clause
  1772.     *make_use_clause(struct token *symbol, struct use_options *options)
  1773. {
  1774.     struct use_clause *res = malloc(sizeof(*res));
  1775.  
  1776.     res->name = parse_symbol_token(symbol);
  1777.     res->options = options->head;
  1778.     res->next = NULL;
  1779.     res->import = NULL;
  1780.     res->exclude = NULL;
  1781.     res->prefix = NULL;
  1782.     res->rename = NULL;
  1783.     res->export = NULL;
  1784.  
  1785.     free(options);
  1786.  
  1787.     return res;
  1788. }
  1789.  
  1790. struct use_options *make_use_options(void)
  1791. {
  1792.     struct use_options *res = malloc(sizeof(*res));
  1793.  
  1794.     res->head = NULL;
  1795.     res->tail = &res->head;
  1796.  
  1797.     return res;
  1798. }
  1799.  
  1800. struct use_options
  1801.     *add_use_option(struct use_options *options, struct use_option *option)
  1802. {
  1803.     *options->tail = option;
  1804.     options->tail = &option->next;
  1805.  
  1806.     return options;
  1807. }
  1808.  
  1809. struct use_option *make_use_option(enum useopt_kind kind)
  1810. {
  1811.     struct use_option *res = malloc(sizeof(*res));
  1812.  
  1813.     res->kind = kind;
  1814.     res->next = NULL;
  1815.  
  1816.     return res;
  1817. }
  1818.  
  1819. struct use_option *make_prefix_option(struct token *token)
  1820. {
  1821.     struct prefix_option *res = malloc(sizeof(*res));
  1822.  
  1823.     res->kind = useopt_PREFIX;
  1824.     res->next = NULL;
  1825.     res->prefix = parse_string_token(token);
  1826.  
  1827.     return (struct use_option *) res;
  1828. }
  1829.  
  1830. struct variable_names *make_variable_names(void)
  1831. {
  1832.     struct variable_names *res = malloc(sizeof(*res));
  1833.  
  1834.     res->head = NULL;
  1835.     res->tail = &res->head;
  1836.  
  1837.     return res;
  1838. }
  1839.  
  1840. struct variable_names
  1841.     *add_variable_name(struct variable_names *names, struct token *token)
  1842. {
  1843.     struct variable_name *new = malloc(sizeof(*new));
  1844.     new->name = parse_symbol_token(token);
  1845.     new->next = NULL;
  1846.  
  1847.     *names->tail = new;
  1848.     names->tail = &new->next;
  1849.     
  1850.     return names;
  1851. }
  1852.  
  1853. struct renamings *make_renamings(void)
  1854. {
  1855.     struct renamings *res = malloc(sizeof(*res));
  1856.  
  1857.     res->head = NULL;
  1858.     res->tail = &res->head;
  1859.  
  1860.     return res;
  1861. }
  1862.  
  1863. struct renamings
  1864.     *add_renaming(struct renamings *names,
  1865.           struct token *from, struct token *to)
  1866. {
  1867.     struct renaming *new = malloc(sizeof(*new));
  1868.  
  1869.     new->from = parse_symbol_token(from);
  1870.     new->to = parse_symbol_token(to);
  1871.     new->next = NULL;
  1872.  
  1873.     *names->tail = new;
  1874.     names->tail = &new->next;
  1875.  
  1876.     return names;
  1877. }
  1878.  
  1879. struct import_option *make_import_option(void)
  1880. {
  1881.     struct import_option *res = malloc(sizeof(*res));
  1882.  
  1883.     res->kind = useopt_IMPORT;
  1884.     res->next = NULL;
  1885.     res->vars = make_variable_names();
  1886.     res->renames = make_renamings();
  1887.  
  1888.     return res;
  1889. }
  1890.  
  1891. struct import_option
  1892.     *add_import(struct import_option *opt,
  1893.         struct token *from, struct token *to)
  1894. {
  1895.     if (to)
  1896.     opt->renames = add_renaming(opt->renames, from, to);
  1897.     else
  1898.     opt->vars = add_variable_name(opt->vars, from);
  1899.  
  1900.     return opt;
  1901. }
  1902.  
  1903.  
  1904. struct use_option *make_exclude_option(struct variable_names *vars)
  1905. {
  1906.     struct exclude_option *res = malloc(sizeof(*res));
  1907.  
  1908.     res->kind = useopt_EXCLUDE;
  1909.     res->next = NULL;
  1910.     res->vars = vars;
  1911.  
  1912.     return (struct use_option *) res;
  1913. }
  1914.  
  1915. struct use_option *make_export_option(struct variable_names *vars)
  1916. {
  1917.     struct export_option *res = malloc(sizeof(*res));
  1918.  
  1919.     res->kind = useopt_EXPORT;
  1920.     res->next = NULL;
  1921.     res->vars = vars;
  1922.  
  1923.     return (struct use_option *) res;
  1924. }
  1925.  
  1926. struct use_option *make_rename_option(struct renamings *lst)
  1927. {
  1928.     struct rename_option *res = malloc(sizeof(*res));
  1929.  
  1930.     res->kind = useopt_RENAME;
  1931.     res->next = NULL;
  1932.     res->renames = lst;
  1933.  
  1934.     return (struct use_option *) res;
  1935. }
  1936.